package view.backend.product;

import exceptionManager.ExceptionManager;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import model.Product;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class AdminProductList 
{
    private JPanel 
            adminProductListPanel,
            scrollPanel;
    private AdminProduct adminProduct;
    private JScrollPane scrollPane;

    public AdminProductList(AdminProduct view)
    {
        adminProduct = view;
        
        adminProductListPanel = new JPanel(null);
        adminProductListPanel.setBounds(320, 0, 580, 500);
        adminProductListPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Fundne Varer", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        adminProductListPanel.setOpaque(false);
        
        scrollPanel = new JPanel(null);
        scrollPanel.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        scrollPanel.setBackground(Color.white);
        scrollPanel.setBorder(null);
        
        scrollPane = new JScrollPane();
        buildExistingScrollPanel("", "ORDER BY productID DESC", "LIMIT 10");
        scrollPane.setBorder(DesignLayout.T_FIELD_RED);
        scrollPane.setBounds(20, 34, 540, 450);
        scrollPane.setViewportView(scrollPanel);        
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        
        adminProductListPanel.add(scrollPane);
        
        adminProductListPanel.setVisible(true);
    }
    
    public void buildExistingScrollPanel(String where, String orderby, String limit)
    {
        scrollPanel.removeAll();
        scrollPane.getViewport().setViewPosition(new Point(0,0));
        
        try
        {
            Product[] products = model.handlers.ProductHandler.getFullProductList(where, orderby, limit);
            
            for (int i = 0; i < products.length; i++)
            {
                AdminProductListPanel panel = new AdminProductListPanel(adminProduct, products[i]);
                panel.setBounds(10, 10 + (i * 190), 500, 180);
                scrollPanel.setPreferredSize(new Dimension(500, 200 + (i * 190)));
                scrollPanel.add(panel);
            }
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
        
        scrollPanel.revalidate();
        scrollPanel.repaint();       
    }
    
    public void reset()
    {
        adminProduct.getPrdSearch().reset();
    }

    public JPanel getPanel()
    {
        return adminProductListPanel;
    }

}